package com.xuexiang.xuidemo.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.StackLayout;
import ohos.agp.components.Text;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import com.xuexiang.xui_lib.component.popupwindow.bar.CookieBar;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.util.AppCfig;
import com.xuexiang.xuidemo.util.ToastUtil;

/**
 * 简易弹出窗
 */
public class CookieBarSlice extends AbilitySlice implements Component.ClickedListener {

    private Text text_title, text_title_bottom;
    private Button btn_top, btn_bottom, btn_top_with_icon, btn_custom;
    private Image image_back;
    private StackLayout container;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_cookie_bar);
        inintComponent();
        initTitle();
        inintClickedListener();

    }

    private void inintComponent() {
        container = (StackLayout) findComponentById(ResourceTable.Id_container);
        text_title = (Text) findComponentById(ResourceTable.Id_text_title);
        text_title_bottom = (Text) findComponentById(ResourceTable.Id_text_title_bottom);
        image_back = (Image) findComponentById(ResourceTable.Id_image_back);
        btn_top = (Button) findComponentById(ResourceTable.Id_btn_top);
        btn_bottom = (Button) findComponentById(ResourceTable.Id_btn_bottom);
        btn_top_with_icon = (Button) findComponentById(ResourceTable.Id_btn_top_with_icon);
        btn_custom = (Button) findComponentById(ResourceTable.Id_btn_custom);
    }

    private void initTitle() {
        text_title.setText("CookieBar");
        text_title_bottom.setText("顶部和底部信息显示条");
    }

    private void inintClickedListener() {
        image_back.setClickedListener(this);
        btn_top.setClickedListener(this);
        btn_bottom.setClickedListener(this);
        btn_top_with_icon.setClickedListener(this);
        btn_custom.setClickedListener(this);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_image_back:
                onBackPressed();
                break;
            case ResourceTable.Id_btn_top:
                CookieBar.builder(getAbility())
                    .setTitle("这是一个标题")
                    .setMessage("这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！")
                    .setLayoutGravity(LayoutAlignment.TOP)
                    .setAction("点击一下", new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            ToastUtil.showToast(getContext(), "点击消失");
                        }
                    })
                    .show(container);

                break;
            case ResourceTable.Id_btn_bottom:
                CookieBar.builder(getAbility())
                    .setTitle("这是一个标题")
                    .setMessage("这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！")
                    .setIcon(ResourceTable.Media_ic_xul)
                    .setLayoutGravity(LayoutAlignment.TOP)
                    .setAction("点击一下", new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            ToastUtil.showToast(getContext(),"点击消失");
                        }
                    })
                    .show(container);
                break;
            case ResourceTable.Id_btn_top_with_icon:
                CookieBar.builder(getAbility())
                    .setTitle("这是一个标题")
                    .setMessage("这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！")
                    .setLayoutGravity(LayoutAlignment.TOP)
                    .setDuration(-1)
                    .setActionWithIcon(ResourceTable.Media_ic_action_close, new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            ToastUtil.showToast(getContext(), "点击消失!");
                        }
                    })
                    .show(container);
                break;
            case ResourceTable.Id_btn_custom:
                CookieBar.builder(getAbility())
                    .setTitle("这是一个标题")
                    .setMessage("这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！这里是提示信息！")
                    .setBackgroundColor(AppCfig.INFO)
                    .setLayoutGravity(LayoutAlignment.TOP)
                    .setDuration(3000)
                    .setAction("点击一下", new Component.ClickedListener() {
                        @Override
                        public void onClick(Component component) {
                            ToastUtil.showToast(getContext(), "点击消失!");
                        }
                    })
                    .show(container);
                break;

        }
    }
}
